<template>
	<div class="system-menu-container">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
				<el-input v-model="querParm.menuName" size="default" placeholder="请输入菜单名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<AuthBtn :add="'system.menu.add'" @onOpenAddMenu="onOpenAddMenu"/>
			</div>

			<div>
				<span style="color:hotpink">衣如雪，剑如墨</span>
			</div>

			<el-table :data="tableData.data" style="width: 100%" row-key="path" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
				<el-table-column label="菜单名称" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon :name="scope.row.meta.icon" />
						<span class="ml10" :style="{'color':scope.row.menuType==0?'':'coral' }">{{ $t(scope.row.meta.title) }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="path" label="路由路径" show-overflow-tooltip></el-table-column>
				<el-table-column label="组件路径" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.component }}</span>
					</template>
				</el-table-column>
				<el-table-column label="权限标识" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.permis }}</span>
					</template>
				</el-table-column>
				<el-table-column label="排序" show-overflow-tooltip width="80">
					<template #default="scope">
						{{ scope.row.menuSort }}
					</template>
				</el-table-column>
				<el-table-column label="类型" show-overflow-tooltip width="80">
					<template #default="scope">
						<el-tag type="success" size="small">{{ scope.row.menuType==0?'菜单':'按钮' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<!-- <el-button size="small" type="text" @click="onOpenAddMenu">新增</el-button>
						<el-button size="small" type="text" @click="onOpenEditMenu(scope.row)">修改</el-button>
						<el-button size="small" type="text" @click="onTabelRowDel(scope.row)">删除</el-button> -->
						<AuthRow :edit="'system.menu.edit'" :delete="'system.menu.delete'" 
						@onOpenAddMenu="onOpenAddMenu" @onOpenEditMenu="onOpenEditMenu(scope.row)" 
						@onTabelRowDel="onTabelRowDel(scope.row)"/>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddMenu ref="addMenuRef" @initTableData="initTableData" />
		<EditMenu ref="editMenuRef" />
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, computed, defineComponent,onMounted } from 'vue';
import { RouteRecordRaw } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import { useStore } from '/@/store/index';
import AddMenu from '/@/views/system/menu/component/addMenu.vue';
import EditMenu from '/@/views/system/menu/component/editMenu.vue';
import AuthBtn from "/@/components/auth/authBtn.vue";
import AuthRow from "/@/components/auth/authRow.vue";
import Auths from "/@/components/auth/auths.vue";
import { menuPermissionApi } from '/@/api/system/index';

const permissionApi = menuPermissionApi();

// 定义接口来定义对象的类型
interface TableDataRow {
	permissionId: string,
	path: string,
	name: string,
	component: string,
	menuSort: string,
	menuType: string,
	menuSuperior: string,
	redirect: string,
	permis: string,
	meta: string,
	children?: TableDataRow[],
}
interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		total: number;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
		};
	};
}

export default defineComponent({
	name: 'systemMenu',
	components: { AddMenu, EditMenu, AuthBtn, AuthRow, Auths },
	setup() {
		const store = useStore();
		const addMenuRef = ref();
		const editMenuRef = ref();
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
				},
			},
			querParm: {
				menuName: "",
				menuId: ""
			}
		});
		// 初始化表格数据
		const initTableData = async () => {
			await permissionApi.getMenuAll({}).then((res)=>{
				state.tableData.data = res.data
			})
		};
		// 打开新增菜单弹窗
		const onOpenAddMenu = () => {
			addMenuRef.value.openDialog(state.tableData.data);
		};
		// 打开编辑菜单弹窗
		const onOpenEditMenu = (row: RouteRecordRaw) => {
			editMenuRef.value.openDialog(row,state.tableData.data);
		};
		// 删除当前行
		const onTabelRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除路由：${row.path}, 是否继续?`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					permissionApi.delete({id:row.permissionId});
					initTableData();
				})
				.catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			addMenuRef,
			editMenuRef,
			onOpenAddMenu,
			onOpenEditMenu,
			initTableData,
			onTabelRowDel,
			...toRefs(state),
		};
	},
});
</script>
